package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.mq.Message;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.BeanMapUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysDeviceData;
import com.ruoyi.system.mapper.SysDeviceDataMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeviceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SysDeviceData)表服务实现类
 *
 * @author makejava
 * @since 2022-03-16 10:44:35
 */
@Service
public class SysDeviceDataServiceImpl implements ISysDeviceDataService {

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysDeviceDataMapper sysDeviceDataDao;

    //spring提供的推送方式
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 通过ID查询单条数据
     *
     * @param deviceDataId 主键
     * @return 实例对象
     */
    @Override
    public SysDeviceData queryById(Long deviceDataId) {
        return this.sysDeviceDataDao.queryById(deviceDataId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SysDeviceData> queryAllByLimit(int offset, int limit) {
        return this.sysDeviceDataDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sysDeviceData 实例对象
     * @return 实例对象
     */
    @Override
    public SysDeviceData insert(SysDeviceData sysDeviceData) {
        this.sysDeviceDataDao.insert(sysDeviceData);
        return sysDeviceData;
    }

    /**
     * 修改数据
     *
     * @param sysDeviceData 实例对象
     * @return 实例对象
     */
    @Override
    public SysDeviceData update(SysDeviceData sysDeviceData) {
        this.sysDeviceDataDao.update(sysDeviceData);
        return this.queryById(sysDeviceData.getDeviceDataId());
    }

    /**
     * 通过主键删除数据
     *
     * @param deviceDataId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long deviceDataId) {
        return this.sysDeviceDataDao.deleteById(deviceDataId) > 0;
    }

    @Override
    public void saveData(String devId, Message message) throws Exception {
        SysDeviceData sysDeviceData = new SysDeviceData();
        sysDeviceData.setDeviceCode(devId);
        sysDeviceData.setDeviceType(1);
        sysDeviceData.setDeviceDataKey(message.getCode());
        sysDeviceData.setDeviceDataValue(message.getValue());
        sysDeviceData.setCreateBy("admin");
        sysDeviceData.setCreateTime(new Date());
        sysDeviceDataDao.insert(sysDeviceData);
        Map<String,String> map = new HashMap<>();
        map.put("devId",devId);
        map.put("code",message.getCode());
        map.put("value",message.getValue());
        messagingTemplate.convertAndSend("/topic/broadcast",map);
    }

    @Override
    public List<SysDeviceData> selectDeviceList(SysDeviceData deviceData) {
        return sysDeviceDataDao.queryAll(deviceData);
    }

    @Override
    public Long addPlantarData(Map<String, Object> plantar) {
        String deviceCode = "";
        Long userId = null;
        for (Map.Entry<String, Object> entry : plantar.entrySet()) {
            if(entry.getKey().equals("deviceCode")){
                deviceCode = entry.getValue().toString();
            }
            if(entry.getKey().equals("recordId")){
                userId = Long.valueOf(entry.getValue().toString());
            }
        }
        Date date = new Date();
        Long deviceDataId = null;
        for (Map.Entry<String, Object> entry : plantar.entrySet()) {
            if(!entry.getKey().equals("deviceCode") && !entry.getKey().equals("recordId")) {
                SysDeviceData sysDeviceData = new SysDeviceData();
                sysDeviceData.setDeviceCode(deviceCode);
                sysDeviceData.setUserId(userId);
                sysDeviceData.setDeviceType(2);
                sysDeviceData.setDeviceDataKey(entry.getKey());
                sysDeviceData.setDeviceDataValue(entry.getValue().toString());
                sysDeviceData.setCreateBy("admin");
                sysDeviceData.setCreateTime(date);
                sysDeviceDataDao.insert(sysDeviceData);
                deviceDataId = sysDeviceData.getDeviceDataId();
                Map<String, String> map = new HashMap<>();
                map.put("devId", deviceCode);
                map.put("code", entry.getKey());
                map.put("value", entry.getValue().toString());
                messagingTemplate.convertAndSend("/topic/broadcast", map);
            }
        }
        return deviceDataId;
    }

    @Override
    public List<Map<String, Object>> queryPlantarData(Long userId, String roleId) {
        List<Map<String,Object>> list = new ArrayList<>();
        if(StringUtils.isNull(userId)){
            userId = Long.valueOf(1024);
            roleId = "1";
        }
        List<SysDeviceData> deviceDataList = new ArrayList<>();
        if(roleId.equals("3")) {
            SysDeviceData deviceData = new SysDeviceData();
            deviceData.setUserId(userId);
            deviceDataList = sysDeviceDataDao.queryAll(deviceData);
        }else{
            deviceDataList = sysDeviceDataDao.queryDataByReceiveUser(userId);
        }
        List<Date> dateList = sysDeviceDataDao.queryTimeByType(2);
        for (Date date : dateList) {
            Map<String,Object> map = new HashMap<>();
            map.put("createBy","admin");
            map.put("createTime",date);
            boolean isExist = false;
            for (SysDeviceData sysDeviceData : deviceDataList) {
                if(date.equals(sysDeviceData.getCreateTime())){
                    map.put("deviceDataId",sysDeviceData.getDeviceDataId());
                    map.put("recordId",sysDeviceData.getUserId());
                    map.put("deviceCode",sysDeviceData.getDeviceCode());
                    map.put(sysDeviceData.getDeviceDataKey(),sysDeviceData.getDeviceDataValue());
                    isExist = true;
                }
            }
            if(isExist) {
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> queryPlantarDataByUser(Long userId) {
        List<Map<String,Object>> list = new ArrayList<>();
        SysUser sysUser = userMapper.selectUserById(userId);
        List<SysDeviceData> deviceDataList = sysDeviceDataDao.queryDataByUser(userId);
        List<Date> dateList = sysDeviceDataDao.queryTimeByType(2);
        for (Date date : dateList) {
            Map<String,Object> map = new HashMap<>();
            map.put("user",sysUser);
            map.put("createBy","admin");
            map.put("createTime",date);
            boolean isExist = false;
            for (SysDeviceData sysDeviceData : deviceDataList) {
                if(date.equals(sysDeviceData.getCreateTime())){
                    map.put("deviceDataId",sysDeviceData.getDeviceDataId());
                    map.put("recordId",sysDeviceData.getUserId());
                    map.put("deviceCode",sysDeviceData.getDeviceCode());
                    map.put(sysDeviceData.getDeviceDataKey(),sysDeviceData.getDeviceDataValue());
                    isExist = true;
                }
            }
            if(isExist) {
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public void addToiletData(String deviceCode, String message) {
        SysDeviceData sysDeviceData = new SysDeviceData();
        sysDeviceData.setDeviceCode(deviceCode);
        sysDeviceData.setDeviceType(3);
        sysDeviceData.setDeviceDataKey("toilet_message");
        sysDeviceData.setDeviceDataValue(message);
        sysDeviceData.setCreateBy("admin");
        sysDeviceData.setCreateTime(new Date());
        sysDeviceDataDao.insert(sysDeviceData);
//        Map<String,Object> map = new HashMap<>();
//        map.put("deviceCode",deviceCode);
//        map.put("deviceDataId",sysDeviceData.getDeviceDataId());
//        messagingTemplate.convertAndSendToUser("toilet","/message",map);
    }
}
